package com.imooc.demo.web;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.imooc.demo.entity.UserAddressinfo;
import com.imooc.demo.service.UserAddressInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/useraddressinfo")
public class UserAddressInfoController {
    @Autowired
    UserAddressInfoService userAddressInfoService;
    // 一对多查询用户地址
   /* @RequestMapping(value = "getuserinfobyopenid",method = RequestMethod.GET)
    private Map<String,Object> getUserInfoByOpenid(String buyerOpenid){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        List<UserAddressinfo> userAddressinfo= userInfoService.getUserInfoByOpenid(buyerOpenid);
        modelMap.put("userAddressinfo",userAddressinfo);
        return modelMap;
    }*/

    //修改种类信息
    @RequestMapping(value = "/updateuseraddress",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等
    private  Map<String,Object>  updateUserAddress(@RequestBody UserAddressinfo userAddressinfo)
            throws JsonParseException, JsonMappingException, IOException {
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        //修改种类信息 ，"success"保存操作状态，如category1Service.modifyCategory1(category1)操作成功返回true，否则false
        modelMap.put("success",userAddressInfoService.updateUserAddress(userAddressinfo));
        return modelMap;
    }

    //删除地址信息
    @RequestMapping(value = "/removeuseraddress",method = RequestMethod.GET)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等
    private  Map<String,Object>  removeUserAddress(Integer addressId)
            throws JsonParseException , JsonMappingException , IOException {
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        //修改种类信息 ，"success"保存操作状态，如category1Service.modifyCategory1(category1)操作成功返回true，否则false
        modelMap.put("success",userAddressInfoService.removeUserAddress( addressId));
        return modelMap;
    }

    //添加地址信息
    @RequestMapping(value = "/insertuseraddressinfo",method = RequestMethod.POST)
    //@RequestBody标识接收的内容除了xwww from url encoding 编码内容之外的一些内容，如json，xml等,此处需传入xml
    private  Map<String,Object>  insertUserAddressinfo(@RequestBody UserAddressinfo userAddressinfo){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        modelMap.put("success",userAddressInfoService.insertUserAddressinfo(userAddressinfo));
        return modelMap;
    }

    @RequestMapping(value = "getaddressinfobyid",method = RequestMethod.GET)
    private  Map<String,Object>  getAddressinfoById(Integer addressId){
        Map<String,Object>  modelMap = new HashMap<String ,Object>();
        UserAddressinfo userAddressinfo= userAddressInfoService.getAddressinfoById( addressId);
        modelMap.put("userAddressinfo",userAddressinfo);
        return modelMap;
    }
}
